'use strict';

angular.module('afasApp')
  .controller('StaffsCtrl', ['$scope', 'Services', '$state', '$filter', function ($scope, Services, $state, $filter) {

    $scope.staffs = [];
    $scope.searchTypes = [
      {
        'Label': 'All',
        'Query': 'staff,teacher,native_teacher'
      },
      {
        'Label': 'Staff',
        'Query': 'staff'
      },
      {
        'Label': 'Indonesian Teacher',
        'Query': 'teacher'
      },
      {
        'Label': 'Native Teacher',
        'Query': 'native_teacher'
      }
    ];
    $scope.roleProfiles = [];
    $scope.staffNew = {
      'StaffType': 0
    };
    $scope.params = {
      'staff_type': 'staff,teacher,native_teacher'
    };

    // Services.getItems($scope.ROLEPROFILE_URL)
    // .then(function(results){
    //   $scope.roleProfiles = results.data;
    // });

    // PAGINATION
    $scope.pagesize = 10;

    $scope.pagination = {
      current: 1
    };

    $scope.pageChanged = function(newPage) {
      getResultsPage(newPage);
    };

    function getResultsPage(pageNumber) {
      var data = $scope.params;
      data.pagesize = $scope.pagesize;
      data.page = (pageNumber - 1);
      $scope.pagination.current = pageNumber;
      Services.getItems('staffs', data)
      .then(function( results ) {
        var pagingData = angular.fromJson(results.headers['x-pagination']);
        $scope.totalItems = pagingData.TotalCount;
        $scope.staffs = results.data;
      });
    }

    getResultsPage(1);

    $scope.addStaff = function () {
      $scope.staffNew.DoB = $filter('date')($scope.timepicker.timeA, 'yyyy-MM-dd');
      Services.add('staffs', $scope.staffNew)
      .then(function(){
        $scope.staffNew = {};
        $state.go( '^.list' );
      }, function(results){
        if (results.status == 400) {
          $scope.errors = results.data;
        }
      });
    };

    $scope.deleteStaff = function (country) {
      Services.delete('staffs', country)
      .then(function(){
        loadRemoteData();
      });
    };
  }]).controller('StaffCtrl', ['$scope', 'Services', '$state', '$stateParams', function ($scope, Services, $state, $stateParams) {

    $scope.currentStaff = {};
    $scope.roleProfiles = [];
    $scope.currentID = $stateParams.id;

    Services.getItems($scope.ROLEPROFILE_URL)
    .then(function(results){
      $scope.roleProfiles = results.data;
    });

    Services.getItem('staffs', $stateParams.id)
    .then(function(results){
      console.log(results);
      $scope.currentStaff = results.data;
      $scope.currentStaff.DoB = $filter('date')($scope.currentStaff.DoB, 'yyyy-MM-dd HH:mm:ss');
      // $scope.currentStaff.DoB = new Date($scope.currentStaff.DoB);
    });

    $scope.updateStaff = function () {
      Services.update('staffs', $scope.currentStaff, $scope.currentID)
      .then(function(){
        $state.go( '^.show', {id: $scope.currentID} );
      });
    };
  }]);